<template>
  <el-dialog
    :close-on-click-modal="false"
    :show-close="false"
    :title="isAdd?'新增标签':'修改标签'"
    :visible.sync="isEdit"
  >
    <el-form
      :rules="rules"
      ref="ruleForm"
      :model="form"
      label-width="120px"
      size="small"
      class="form-box"
    >
      <!-- <el-form-item label="昵称" prop="order">
        <el-input type="text" v-model="form.order" placeholder="请输入昵称"></el-input>
      </el-form-item> -->
      <el-form-item label="标签名字" prop="tag">
        <el-input type="text" v-model="form.tag" placeholder="请输入标签名字"></el-input>
      </el-form-item>
      <el-form-item label="标签图标">
        <uploadImg @bindSendImg="propImg" :imgUrl="form.avatar"></uploadImg>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="text" v-model="form.desc" placeholder="请输入备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('closeDialog')" size="small">取消</el-button>
      <el-button @click="submitFormAdd('ruleForm')" type="primary" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script lang="ts">
import { Component, Vue, Prop, Provide } from "vue-property-decorator";
import goodsApi from "@/api/goods";
import uploadImg from "@/components/uploadImg/index.vue";
@Component({
  components: {uploadImg}
})
export default class supplierEditDialog extends Vue {
  // 属性或参数中使用 ！：表示强制解析（告诉typescript编译器，这里一定有值），常用于vue-decorator中的@Prop
  check:boolean = true//防重复提交
  @Prop(Boolean) isEdit!: boolean;
  @Prop(Boolean) isAdd!: boolean;
  @Prop(Boolean) isMenu!: boolean;
  @Prop(Object) form!:{
    id:number,
    avatar: string,
    order:string,
    tag:string,
    desc:string,
  }
  @Provide() rules: any = {
    // order: [{ required: true, message: "请输昵称", trigger: "blur" }],
    tag: [{ required: true, message: "请输入标签名字", trigger: "blur" }],
  };

  // 取消
  cancel(){
    this.$router.push({
      path:'/labelList'
    })
  }
  // 上传图片子组件传递过来的图片路径
  propImg(imgPath: any){
    this.form.avatar = imgPath;
  }
  // 提交
  submitFormAdd(formName: any) {
    
    (this.$refs[formName] as any).validate((valid: boolean) => {
      if (valid) {
        if(this.check) {
          let _this= this
          _this.check = false
          goodsApi.savelabel(this.form)
          .then((res: any) => {
            if(res.data.code==200){
              _this.$message({
                message: '操作成功',
                type: "success"
              });
              _this.$emit("closeDialog");
              _this.$emit("loadData")
              setTimeout(function(){
                _this.check = true
              },1500)
            }else{
              _this.$message({
                message: res.data.msg,
                type: "error"
              });
              _this.check = true
            }
          });
        }
          
      }
    });
  }
  created() {
  }
}
</script>

<style lang="scss" scoped>
.form-box {
  .el-input,
  .el-select {
    width: 200px !important;
  }
}
</style>
